package com.itheima.web.controller.sms;

import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.sms.FlashPromotionProductRelationService;
import com.itheima.vo.sms.SmsFlashPromotionProductRelation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动关联商品
 */
@RestController
@RequestMapping("flashProductRelation")
@Slf4j
public class FlashPromotionProductRelationController {

    @DubboReference
    private FlashPromotionProductRelationService flashPromotionProductRelationService;

    /**
     * 批量选择商品添加关联
     *
     * @param relations
     * @return
     */
    @PostMapping("create")
    public ResultData create(@RequestBody List<SmsFlashPromotionProductRelation> relations) {
        log.info("批量选择商品添加关联信息：{}", relations);
        return flashPromotionProductRelationService.create(relations);
    }

    /**
     * 删除关联
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResultData delete(@PathVariable Long id) {
        log.info("要删除关联的id：{}", id);
        int result = flashPromotionProductRelationService.delete(id);
        return ResultData.ok("操作成功", result);
    }

    /**
     * 分页查询不同场次关联及商品信息
     *
     * @param pageNum
     * @param pageSize
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @return
     */
    @GetMapping("list")
    public ResultData list(Long pageNum, Long pageSize, Long flashPromotionId, Long flashPromotionSessionId) {
        log.info("分页查询不同场次关联及商品信息条件：{}", pageNum, pageSize, flashPromotionId, flashPromotionSessionId);
        PageResult pageResult = flashPromotionProductRelationService.seleteAll(pageNum, pageSize, flashPromotionId, flashPromotionSessionId);
        return ResultData.ok("操作成功", pageResult);
    }

    /**
     * 修改关联信息
     *
     * @param smsFlashPromotionProductRelation
     * @return
     */
    @PostMapping("update/{id}")
    public ResultData update(@PathVariable Long id, @RequestBody SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        log.info("修改关联信息：{},{}", id, smsFlashPromotionProductRelation);
        int result = flashPromotionProductRelationService.updateRelation(id, smsFlashPromotionProductRelation);
        return ResultData.ok("操作成功", result);
    }

    /**
     * 获取管理商品促销信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    private ResultData get(@PathVariable Long id) {
        log.info("获取管理商品促销信息的id：{}", id);
        SmsFlashPromotionProductRelation relation = flashPromotionProductRelationService.get(id);
        return ResultData.ok("操作成功", relation);
    }
}
